/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package persistencia;

import java.sql.ResultSet;

/**
 *
 * @author carlos
 */
public abstract class InterfazPersistencia 
{
    private Conexion conexion=Conexion.getConexion();
    protected abstract String getTabla();    
    protected abstract String[][] getDatos();
    public  String primaryKey;

    public String getIntefazTabla()
    {
        return getTabla();
    }
    
    public String[][] getInterfazDatos()
    {
        return getDatos();
    }
    
    public InterfazPersistencia(String primaryKey) 
    {
       this.primaryKey=primaryKey;
    }
    
    public InterfazPersistencia(int primaryKey) 
    {
       this.primaryKey=primaryKey+"";
    }
    
    public InterfazPersistencia(float primaryKey) 
    {
       this.primaryKey=primaryKey+"";
    }
    
        
    public void grabar()
    {
       String consulta="INSERT INTO "+this.getTabla()+"(";
       String[][] vector=this.getDatos();
       
    
       for (int index = 0; index < vector.length; index++) 
       {
           if(index==vector.length-1)
               consulta=consulta+vector[index][0];    
           else     
                consulta=consulta+vector[index][0]+",";
       }
       consulta=consulta+") VALUES (";
       
       
       for (int index = 0; index < vector.length; index++) 
       {
           if(index==vector.length-1)
               consulta=consulta+"'"+vector[index][1]+"'";    
           else     
               consulta=consulta+"'"+vector[index][1]+"',";
       }
       
       consulta=consulta+");";
       //echo "->".$consulta."<-";
       conexion.update(consulta);
       
       
   
    }
    
    public void editar()
    {
        String consulta = "UPDATE " + this.getTabla() + " SET ";
        String [][]vector = this.getDatos();
        
        for (int index = 0; index < vector.length; index++) {
            if (index == vector.length - 1)
                consulta = consulta +vector[index][0] + "='" + vector[index][1] + "'";
            else
                consulta = consulta + vector[index][0] + "='" + vector[index][1] + "'" + ",";
        }
        consulta = consulta + " WHERE " + vector[0][0] + "='" + this.primaryKey+ "';";
        // echo $consulta;            
        //$Result1 = $this->conexion->ejecutarConsulta($consulta, $this->conexion) or die(mysql_error());
        conexion.update(consulta);
        //return $Result1;
    
    }
    
    public void eliminar()
    {
        String [][]vector= this.getDatos();
        String consulta = "DELETE FROM " + this.getTabla();
        consulta = consulta + " WHERE " + vector[0][0] + "='" + vector[0][1] + "';";
        //echo $consulta;
        conexion.update(consulta);
        
    }
    
    public ResultSet consultaTodos()
    {
        String consulta = "SELECT * FROM "+this.getTabla();
        return conexion.select(consulta);
    }
    
    public ResultSet consultaUno()
    {
        String [][]vector=this.getDatos();
        String consulta = "SELECT * FROM "+this.getTabla()+" WHERE "+vector[0][0]+"='"+vector[0][1]+"'";
        //$vector[0][0] . "='" . $vector[0][1] . "'
        return conexion.select(consulta);
    }
    
    public static ResultSet consultaLibre(String consulta)
    {
        Conexion conexion=Conexion.getConexion();
        return conexion.select(consulta);
    }
    
    
    
    public void setPrimaryKey(String key)
    {
        this.primaryKey=key;
    }
    
    
}
